package ar.edu.itba.it.pdc.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;

import org.joda.time.DateTime;

import ar.edu.itba.it.pdc.app.Madonna3;
import ar.edu.itba.it.pdc.app.client.Client;
import ar.edu.itba.it.pdc.app.config.mapping.EraseCondition;
import ar.edu.itba.it.pdc.protocol.POP3Request;
import ar.edu.itba.it.pdc.protocol.POP3Response;
import ar.edu.itba.it.pdc.protocol.ValidPOP3Response;
import ar.edu.itba.it.pdc.protocol.email.Header;
import ar.edu.itba.it.pdc.protocol.email.Mail;

public class DeleteConditions {
	
	private final static String CRLF = TempFilesSetup.CRLF;
	
	private static List<EraseCondition> conditions = Madonna3.getApp().getSettings().getProxy().getEraseConditions().getEraseCondition();

	public static boolean checkConditions(Client client, Socket socket, int n) throws IOException {
		Integer daysOld = null;
		String senderList = null;
	    String patternHeader = null;
	    Integer maxSize = null;
	    String structure = null;
	    String contentType = null;
		
		for (EraseCondition c : conditions)
			if (c.getUserList() != null && c.getUserList().contains(client.getUsername())){
				daysOld = c.getDaysOld();
				senderList = c.getSenderList();
				patternHeader = c.getPatternHeader();
				maxSize = c.getMaxSize();
				structure = c.getStructure();
				contentType = c.getContentType();
			}		
		
		POP3Request req = POP3Request.build("RETR " + n + CRLF);
		req.sendTo(socket);

		POP3Response resp = POP3Response.build(socket.getInputStream());
		
		if (!req.isValid() || !resp.isValid())
			return false;
		
		ValidPOP3Response response = (ValidPOP3Response) resp;		
		
		Mail mail = Mail.build(new FileInputStream(response.getResponse()));
		
		if (daysOld != null){
			DateTime now = new DateTime();
			DateTime date = mail.getDate();
			if (date.plusDays(daysOld).isAfter(now)) {
				Madonna3.log.info("Deletion restricted by daysOld");
				return false;
			}
		}
		
		if (senderList != null){
			String from = mail.getHeader("From").getValue();
			String[] senders = senderList.split(",");
			for (int i=0; i < senders.length; i++) {
				if (from.contains(senders[i])) {
					Madonna3.log.info("Deletion restricted by senderList");
					return false;
				}
			}
		}
		
		if(patternHeader != null){
			String[] pattern = patternHeader.split("\\s+", 3);
			String headerPattern = pattern[0];
			String op = pattern[1];
			String value = pattern[2];
			
			Header header = mail.getHeader(headerPattern);
			
			if (header != null){
				if (op.equals("eq")){
					if (header.getValue().equals(value)) {
						Madonna3.log.info("Deletion restricted by patternHeader");
						return false;
					}
				}
			}				
		}
		
		if (maxSize != null && mail.getSize() > maxSize){
			Madonna3.log.info("Deletion restricted by maxSize");
			return false;
		}

		if (structure != null){
			if(structure.toLowerCase().equals("adjuntos")) {
				if(mail.getContentType() != null && mail.getContentType().toLowerCase().contains("multipart")) {
					Madonna3.log.info("Deletion restricted by attachment");
					return false;
				}
			}
		}
		
		if(contentType != null){
			if(mail.getContentType() != null) {
				if(mail.getContentType().toLowerCase().contains(contentType.toLowerCase())) {
					Madonna3.log.info("Deletion restricted by content-type");
					return false;
				}
			}
		}
	
		return true;
	}
	
}
